import 'package:flutter/material.dart';
import 'dart:ui' as ui;

class SmoothLineChart extends StatelessWidget {
  // final List<double> data;

  const SmoothLineChart({super.key});

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: LineChartPainter(),
    );
  }
}

class LineChartPainter extends CustomPainter {
  final List<double> data = [5, 8, 10, 7, 9, 6, 3];

  Paint linePaint = Paint()
    ..color = Colors.blue
    ..strokeWidth = 2.0
    ..style = PaintingStyle.stroke;

  Path linePath = Path();

  @override
  void paint(Canvas canvas, Size size) {
    Paint linePaint = Paint()
      ..color = Colors.blue
      ..strokeWidth = 2.0
      ..style = PaintingStyle.stroke;

    Path path = _createSmoothPath(size);

    //绘制曲线图
    canvas.drawPath(path, linePaint);

    //绘制双柱状图

    //绘制XY轴

    //X轴单位

    //Y轴单位
  }

  Path _createSmoothPath(Size size) {
    Path path = Path();

    if (data.length < 2) {
      return path;
    }

    double width = size.width;
    double height = size.height;

    path.moveTo(0, height - (data[0] / _getMaxValue()) * height);

    for (int i = 0; i < data.length - 1; i++) {
      double x1 = i * (width / (data.length - 1));
      double y1 = height - (data[i] / _getMaxValue()) * height;
      double x2 = (i + 1) * (width / (data.length - 1));
      double y2 = height - (data[i + 1] / _getMaxValue()) * height;

      double xc = (x1 + x2) / 2;
      double yc = (y1 + y2) / 2;

      path.quadraticBezierTo(x1, y1, xc, yc);
    }

    return path;
  }

  double _getMaxValue() {
    // return data.reduce(max);
    return 15;
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}
